/*
 * CategoryPresenter.java
 * business
 *
 * Created by SunDing on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.category;

import android.support.v4.util.ArrayMap;
import android.text.TextUtils;

import com.yidiandao.business.data.imagemodule.ImageAD;
import com.yidiandao.business.data.model.GoodsModel;
import com.yidiandao.business.data.model.IGoodsModel;
import com.yidiandao.business.data.response.CategoryResponse;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.CategoryActiveListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.CategoryListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.CategoryTreeAddressItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.CategoryTreeInfoItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.CategoryTreeTitleItem;
import com.yidiandao.business.ui.widget.view.SuperLoadingLayout;

import java.util.ArrayList;
import java.util.List;

/** 分类Presenter */
public class CategoryPresenter implements CategoryContract.Presenter {

    private final GoodsModel mGoodsModel;
    private CategoryContract.View mCategoryView;
    private ArrayList<DisplayListItem> mNames = new ArrayList<>(); //存放大品牌名
    private List<CategoryResponse.DataBean> mData = new ArrayList<>();
    private List<CategoryResponse.DataBean.CatListBean> mCatList = new ArrayList<>();

    public CategoryPresenter(CategoryContract.View view) {
        mCategoryView = view;
        mCategoryView.setPresenter(this);

        mGoodsModel = new GoodsModel();
    }

    @Override
    public void start() {
        mNames.clear();
        mData.clear();
        mCatList.clear();
        getData();
    }

    @Override
    public void getData() {
        mGoodsModel.goodsCategory(
                mCategoryView.getPagerTag(),
                new IGoodsModel.onGoodsCategoryListener() {

                    @Override
                    public void onFailed() {
                        mCategoryView.refreshContent(SuperLoadingLayout.STATE_ERROR);
                    }

                    @Override
                    public void onFinal() {
                    }

                    @Override
                    public void onSuccess(List<CategoryResponse.DataBean> data) {
                        mCategoryView.refreshContent(mCategoryView.checkResultObject(data));
                        mData.addAll(data);
                        if (mData.size() > 0) {
                            //存放品牌名
                            for (int i = 0; i < mData.size(); i++) {
                                CategoryResponse.DataBean dataBean = mData.get(i);
                                //获取品牌名
                                String name = dataBean.getName();
                                ArrayMap<String, String> map = new ArrayMap<>();
                                map.put("name", name);
                                map.put("check", "0");
                                CategoryListItem item = new CategoryListItem(map);
                                mNames.add(item);
                            }
                            mCategoryView.setCategoryBrand(mNames);
                            //默认选中第一条
                            setData(0);
                        }
                    }
                });
    }

    @Override
    public void setData(int position) {
        for (int i = 0; i < mData.size(); i++) {
            CategoryListItem displayListItem = (CategoryListItem) mNames.get(i);
            ArrayMap<String, String> map = displayListItem.getContentData();
            map.put("check", i == position ? "1" : "0");
        }
        mCategoryView.refreshUI();

        mCatList.clear();
        //遍历数组
        CategoryResponse.DataBean dataBean = mData.get(position);

        //获取每个品牌下的分支
        mCatList.addAll(dataBean.getCatList());
        ArrayList<DisplayListItem> mInfos = new ArrayList<>(); //内容
        ImageAD adverInfo = dataBean.getImageAD();
        if (null != adverInfo && !TextUtils.isEmpty(adverInfo.getImage())) {
            mInfos.add(new CategoryActiveListItem(adverInfo));
        }
        if (mCatList.size() != 0) {
            for (int j = 0; j < mCatList.size(); j++) {
                CategoryResponse.DataBean.CatListBean catListBean = mCatList.get(j);
                String brand = catListBean.getBrand();
                //品名
                if (j != 0) mInfos.add(new CategoryTreeTitleItem(brand));
                //内容
                List<CategoryResponse.DataBean.CatListBean.Cat> cats = catListBean.getCat();
                //当只有品牌时
                if (j == 0) {
                    for (int i = 0; i < cats.size(); i += 3) {
                        List<CategoryResponse.DataBean.CatListBean.Cat> cat;
                        if (i + 2 >= cats.size()) {
                            cat = cats.subList(i, cats.size());
                        } else {
                            cat = cats.subList(i, i + 3);
                        }
                        mInfos.add(new CategoryTreeInfoItem(cat));
                    }
                }
                //当存在产地时
                if (mCatList.size() >= 2 && j >= 1) {
                    mInfos.add(new CategoryTreeAddressItem(cats));
                }
            }
        }
        mCategoryView.setCategoryInfo(mInfos);
    }
}
